//
//  SERegistrationDateViewModel.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/21.
//

import HandyJSON

class SERegistrationDateViewModel: SEBaseViewModel {
    var dateModels: [SERegistrationDateModel] = []
    var doctorListModels: [SERegistrationDoctorModel] = []

    var hospitalId = ""
    var departmentId = ""
    var currentDateModel = SERegistrationDateModel()

    var dateHandler: ((SEHttpStatus) -> Void)?

    func doctorModel(section: Int) -> SERegistrationDoctorModel {
        return doctorListModels[section]
    }

    func doctorShift(indexPath: IndexPath) -> SERegistrationDoctorShift {
        return doctorModel(section: indexPath.section).shiftList[indexPath.row]
    }

    var numberOfSection: Int {
        return doctorListModels.count + 1
    }

    var lastSection: Int {
        return numberOfSection - 1
    }

    func numberOfRows(section: Int) -> Int {
        if section == lastSection {
            return 0
        } else {
            let model = doctorModel(section: section)
            if model.isOpen {
                return model.shiftList.count
            } else {
                if model.shiftList.count <= 2 {
                    return model.shiftList.count
                } else {
                    return 2
                }
            }
        }
    }

    func heightForRow(indexPath: IndexPath) -> CGFloat {
        if indexPath.section == lastSection {
            return 102
        }
        return UITableView.automaticDimension
    }

    func heightForHeader(section: Int) -> CGFloat {
        if section == lastSection {
            return numberOfRows(section: lastSection) == 0 ? 0 : 46
        }
        return 79
    }

    func heightForFooter(section: Int) -> CGFloat {
        if section == lastSection {
            return 0
        } else {
            let model = doctorModel(section: section)
            if model.shiftList.count <= 2 {
                return 10
            } else {
                return 30
            }
        }
    }

    func requestDateArray() {
        SEHttpManager.shared.requestForModelArray(SERegistrationAPI.dateList(hospitalId: hospitalId, departmentId: departmentId), model: SERegistrationDateModel.self) { (models, status) in
            switch status {
            case .success:
                if var models = models {
                    models.insert(SERegistrationDateModel(), at: 0)
                    self.dateModels = models
                    self.dateHandler?(status)
                    self.requestRegisterDoctorList()
                }
            default:
                self.dateHandler?(status)
            }
        }
    }

    func requestRegisterDoctorList() {
        SEHttpManager.shared.requestForModelArray(SERegistrationAPI.registrationDoctorList(hospitalId: hospitalId, deptId: departmentId, startDate: self.currentDateModel.dateTime, endDate: "", registerType: "", doctorName: "", pageNum: 1, pageSize: 1000), model: SERegistrationDoctorModel.self) { (models, status) in
            switch status {
            case .success:
                if let models = models {
                    self.doctorListModels = models
                    self.updateData?(status)
                }
            default:
                self.updateData?(status)
            }

        }
    }

}

/// 日期导航
struct SERegistrationDateModel: HandyJSON {
    /**
     科室id
     */
    var deptId: String = ""
    /**
     周几
     */
    var week: String = ""
    /**
     日期 - 页面展示用
     */
    var time: String = ""
    /**
     日期 - 筛选排班提交接口用
     */
    var dateTime: String = ""
    /**
     有无号源 - 0: 无, 1: 有
     */
    var val: Int = 0
}

/// 医生信息
struct SERegistrationDoctorModel: HandyJSON {
    /**
     科室id
     */
    var deptId: String = ""
    /**
     科室名称
     */
    var deptName: String = ""
    /**
     医生id
     */
    var doctorId: String = ""
    /**
     医生姓名
     */
    var doctorName: String = ""
    /**
     擅长
     */
    var goodAt: String = ""
    /**
     医院id
     */
    var hospitalId: String = ""
    /**
     医院名称
     */
    var hospitalName: String = ""
    /**
     医生职称
     */
    var jobTitle: String = ""
    /**
     判断是否有值。有就是注册了图文问诊，可跳转医生详情，使用此id查询医生详情
     */
    var inquirydocId: String = ""
    /**
     排班列表
     */
    var shiftList: [SERegistrationDoctorShift] = []
    /**
     医生头像
     */
    var picUrl: String = ""
    /**
     是否缩放排班布局
     */
    var isOpen: Bool = false
    var defalultLines: Int = 1
}

/// 医生排班
struct SERegistrationDoctorShift: HandyJSON {
    /**
     排班号源id
     */
    var id: String = ""
    /**
     科室名称
     */
    var deptName: String = ""
    /**
     医生姓名
     */
    var doctorName: String = ""
    /**
     排班日期
     */
    var shiftDate: String = ""
    /**
     上午/下午
     */
    var registerPeriod: String = ""
    /**
     费用
     */
    var fee: String = ""
    /**
     剩余可预约数量
     */
    var registerNumber: Int = 0
}
